<?php
require_once('includes.php');

/*
 * Get vehicle year
 */
function year_select() {
  $car = new Vehicle();
  $car->display_year();
}

/*
 * Get vehicle makers
 */
if (isset($_GET['func']) && $_GET['func'] == "drop_1" ) {
  drop_1($_GET['vehicleYear']);
}
function drop_1($year) {
  $car = new Vehicle();
  $car->display_makers($year);
}


/*
 * Get vehicle models
 */
if ( isset($_GET['func']) && $_GET['func'] == "drop_2") {
  drop_2($_GET['vehicleYear'], $_GET['vehicleMaker']);
}

function drop_2($year, $maker) {
  $car = new Vehicle();
  $car->display_models($year, $maker);
}

/*
 * Get vehicle transmission
 */
if ( isset($_GET['func']) && $_GET['func'] == "drop_3") {
  drop_3($_GET['vehicleYear'], $_GET['vehicleMaker'],$_GET['vehicleModel']);
}

function drop_3($year, $maker,$model) {
  $car = new Vehicle();
  $car->display_transmission($year, $maker,$model);
}

if ( isset($_GET['func']) && $_GET['func'] == "drop_4") {
  drop_4();
}

function drop_4() {
  $car = new Vehicle();
  $car->display_button();
}


/*
 * Get national gas prices from the department of energy
 */
function get_national_prices() {
  $url = 'http://www.fueleconomy.gov/ws/rest/fuelprices';
  $xml = file_get_contents($url);
  $prices = simplexml_load_string($xml);
  return $prices;
}
/*
 * Get POST data
 * year, make, model, transmission
 */
function get_post_data() {
  $car = array(
    'year' => '',
    'make' => '',
    'model' => '',
    'transmission' => ''
  );
  if(isset($_POST['submit'])){
    //Form values year, make, model, transmission
	$car['year'] = $_POST['drop_1'];
	$car['make'] = $_POST['drop_2'];
    $car['model'] = $_POST['drop_3'];
	$car['transmission'] = $_POST['drop_4'];
    return $car;
  }
}

/*
 * Get car info from database
 * returns object with
 * Engine size, transmission, cylinders, MPG
 */
function get_car_info() {
  $car = get_post_data();

  //fetch data from database
  $carObj = new Database();
  $car_info = $carObj->get_all($car['year'],$car['make'],$car['model'],$car['transmission']);

  return $car_info;
}

/*
 *
 */
function display_vehicle_info() {
  if(isset($_POST['submit'])){
    $car_info = get_car_info();
    //display vehicle data
    echo "<div class='vehicle-title'>";
    echo $car_info['modelYear']." ".$car_info['mfrName']. " " . $car_info['carline'] . "</br>";
    echo "</div>";
    display_car_info($car_info);
  }
}

function display_gas_info() {
  if (isset($_POST['submit'])) {
    echo '$' . $_POST['gas_price'];
  }
}

/*
 * Display car info
 */
function display_car_info($car_info) {
  echo "<div class='vehicle-detail'>";
  echo $car_info['engDispl'] . " L, ";
  echo $car_info['cylinders'] . " cyl, ";
  echo $car_info['transmission'] . "</br>";
  echo "City: " . $car_info['city'] . " MPG</br>";
  echo "Highway: " . $car_info['highway'] . " MPG</br>";
  echo "Combined: " . $car_info['combined'] . " MPG</br>";
  echo "</div>";
}

/*
 * Calculate weekly cost of fuel
 */
function weekly_cost($car_info, $price) {
  $weekly_cost = array(
    'city' => '',
    'highway' => '',
    'combined' => '',
  );
  $city_price_per_mile = $price / $car_info['city'];
  $weekly_cost['city'] = $city_price_per_mile * $_POST['miles'];

  $highway_price_per_mile = $price / $car_info['highway'];
  $weekly_cost['highway'] = $highway_price_per_mile * $_POST['miles'];

  $combined_price_per_mile = $price / $car_info['combined'];
  $weekly_cost['combined'] = $combined_price_per_mile * $_POST['miles'];

  return $weekly_cost;
}

/*
 * Projected fuel cost
 * calculates the with a 1% increase per week
 * Returns an array with city, highway and combined
 */
function projected_cost($weekly_cost, $weeks) {
  $projected_cost = array(
    'city' => '',
    'highway' => '',
    'combined' => '',
  );
  //Increase cost of gas by 1%
  for($i = 1; $i <= $weeks; $i++ ) {
    foreach($weekly_cost as $k => $v) {
      $v *= 1.001;
      if ($i == 1) {
        $projected_cost[$k] = $weekly_cost[$k];
      } else {
        $weekly_cost[$k] = $v;
        $projected_cost[$k] += $v;
      }
    }
  }
  return $projected_cost;
}

/*
 * Display fuel cost
 */
function display_fuel_cost() {
  if (isset($_POST['submit'])) {
    //set US currency
    setlocale(LC_MONETARY, 'en_US');
    //Get vehicle information
    $car_info = get_car_info();

    //Display fuel cost
    $weekly_cost = weekly_cost($car_info, $_POST['gas_price']);
    $yearly_cost = projected_cost($weekly_cost, 52);
    $yearly_city_cost = money_format('%(#10n', $yearly_cost['city']);
    $yearly_highway_cost = money_format('%(#10n', $yearly_cost['highway']);
    $yearly_combined_cost = money_format('%(#10n', $yearly_cost['combined']);

    $five_year_cost = projected_cost($weekly_cost, 260);
    $five_year_city_cost = money_format('%(#10n', $five_year_cost['city']);
    $five_year_highway_cost = money_format('%(#10n', $five_year_cost['highway']);
    $five_year_combined_cost = money_format('%(#10n', $five_year_cost['combined']);
    echo "Your Projected Fuel Cost </br>";
    echo "
      <table class='tg-table-light'>
        <tr>
          <th></th>
          <th>City</th>
          <th>Highway</th>
          <th>Combined</th>
        </tr>
        <tr class='tg-even'>
          <td>Yearly Cost</td>
          <td>$yearly_city_cost</td>
          <td>$yearly_highway_cost</td>
          <td>$yearly_combined_cost</td>
        </tr>
        <tr>
          <td>5 years</td>
          <td>$five_year_city_cost</td>
          <td>$five_year_highway_cost</td>
          <td>$five_year_combined_cost</td>
        </tr>
        <tr class='tg-even'>
        </tr>
      </table>";

  }
}

/*
 * Display fuel cost
 */
function display_national_fuel_cost() {
  if (isset($_POST['submit'])) {
    //set US currency
    setlocale(LC_MONETARY, 'en_US');
    //Get vehicle information
    $car_info = get_car_info();
    $prices = get_national_prices();

    //Display National fuel cost regular gas
    $weekly_cost_regular = weekly_cost($car_info, (float)$prices->regular);
    $yearly_cost_regular = projected_cost($weekly_cost_regular, 52);
    $yearly_city_cost = money_format('%(#10n', $yearly_cost_regular['city']);
    $yearly_highway_cost = money_format('%(#10n', $yearly_cost_regular['highway']);
    $yearly_combined_cost = money_format('%(#10n', $yearly_cost_regular['combined']);

    //Display National fuel cost Mid grade gas
    $weekly_cost_midgrade = weekly_cost($car_info, (float)$prices->midgrade);
    $yearly_cost_midgrade = projected_cost($weekly_cost_midgrade, 52);
    $yearly_city_cost_midgrade = money_format('%(#10n', $yearly_cost_midgrade['city']);
    $yearly_highway_cost_midgrade = money_format('%(#10n', $yearly_cost_midgrade['highway']);
    $yearly_combined_cost_midgrade = money_format('%(#10n', $yearly_cost_midgrade['combined']);

    //Display National fuel cost Mid grade gas
    $weekly_cost_premium = weekly_cost($car_info, (float)$prices->premium);
    $yearly_cost_premium = projected_cost($weekly_cost_premium, 52);
    $yearly_city_cost_premium = money_format('%(#10n', $yearly_cost_premium['city']);
    $yearly_highway_cost_premium = money_format('%(#10n', $yearly_cost_premium['highway']);
    $yearly_combined_cost_premium = money_format('%(#10n', $yearly_cost_premium['combined']);

    echo "National Average Yearly Cost </br>";
    echo "
      <table class='tg-table-light'>
        <tr>
          <th></th>
          <th>City</th>
          <th>Highway</th>
          <th>Combined</th>
        </tr>
        <tr class='tg-even'>
          <td>Regular</td>
          <td>$yearly_city_cost</td>
          <td>$yearly_highway_cost</td>
          <td>$yearly_combined_cost</td>
        </tr>
        <tr>
          <td>Mid grade</td>
          <td>$yearly_city_cost_midgrade</td>
          <td>$yearly_highway_cost_midgrade</td>
          <td>$yearly_combined_cost_midgrade</td>
        </tr>
        <tr class='tg-even'>
          <td>Premium</td>
          <td>$yearly_city_cost_premium</td>
          <td>$yearly_highway_cost_premium</td>
          <td>$yearly_combined_cost_premium</td>
        </tr>
      </table>";
  }
}
